import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 车辆类
abstract class Vehicle {
    protected String licensePlate;
    protected String brand;
    protected boolean isRented;
    // 新增租车天数属性
    protected int rentDays;

    public Vehicle(String licensePlate, String brand) {
        this.licensePlate = licensePlate;
        this.brand = brand;
        this.isRented = false;
        this.rentDays = 0;
    }

    public String getLicensePlate() {
        return licensePlate;
    }

    public String getBrand() {
        return brand;
    }

    public boolean isRented() {
        return isRented;
    }

    public int getRentDays() {
        return rentDays;
    }

    public void rent() {
        isRented = true;
    }

    public void returnCar() {
        isRented = false;
    }

    // 设置租车天数的方法
    public void setRentDays(int days) {
        this.rentDays = days;
    }
}

// 汽车类
class Car extends Vehicle {
    public Car(String licensePlate, String brand) {
        super(licensePlate, brand);
    }
}

// 卡车类
class Truck extends Vehicle {
    public Truck(String licensePlate, String brand) {
        super(licensePlate, brand);
    }
}

// 巴士类
class Bus extends Vehicle {
    public Bus(String licensePlate, String brand) {
        super(licensePlate, brand);
    }
}

// 用户类
abstract class User {
    protected String username;
    protected String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public boolean login(String inputUsername, String inputPassword) {
        return username.equals(inputUsername) && password.equals(inputPassword);
    }

    public abstract void operate(List<Vehicle> vehicleList);
}

// VIP用户类
class VIPUser extends User {
    private Scanner scanner;

    public VIPUser(String username, String password) {
        super(username, password);
        this.scanner = new Scanner(System.in);
    }

    @Override
    public void operate(List<Vehicle> vehicleList) {
        boolean loop = true;
        while (loop) {
            System.out.println("VIP用户操作菜单：");
            System.out.println("1. 租车");
            System.out.println("2. 还车");
            System.out.println("3. 付款");
            System.out.println("4. 退出");
            try {
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice) {
                    case 1:
                        rentCar(vehicleList);
                        break;
                    case 2:
                        returnCar(vehicleList);
                        break;
                    case 3:
                        payRent(vehicleList);
                        break;
                    case 4:
                        loop = false;
                        break;
                    default:
                        System.out.println("无效选择，请重新输入。");
                }
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入。");
                scanner.nextLine();
            }
        }
    }

    private void rentCar(List<Vehicle> vehicleList) {
        if (vehicleList.size() > 0) {
            System.out.println("可租用车辆：");
            for (Vehicle vehicle : vehicleList) {
                if (!vehicle.isRented()) {
                    System.out.println("车辆类型：" + vehicle.getClass().getSimpleName() + "，车牌：" + vehicle.getLicensePlate() + "，品牌：" + vehicle.getBrand());
                }
            }
            System.out.println("请输入要租用的车辆车牌：");
            String rentPlate = scanner.nextLine();
            for (Vehicle vehicle : vehicleList) {
                if (vehicle.getLicensePlate().equals(rentPlate)) {
                    if (!vehicle.isRented()) {
                        vehicle.rent();
                        System.out.println("请输入租车天数：");
                        int days = scanner.nextInt();
                        scanner.nextLine();
                        vehicle.setRentDays(days);
                        System.out.println("成功租用车辆：" + vehicle.getLicensePlate() + "，租车天数：" + days);
                        return;
                    } else {
                        System.out.println("车辆已被租用，请选择其他车辆。");
                        return;
                    }
                }
            }
            System.out.println("未找到指定车辆。");
        } else {
            System.out.println("无可用车辆。");
        }
    }

    private void returnCar(List<Vehicle> vehicleList) {
        System.out.println("请输入要归还的车辆车牌：");
        String returnPlate = scanner.nextLine();
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.getLicensePlate().equals(returnPlate)) {
                if (vehicle.isRented()) {
                    vehicle.returnCar();
                    System.out.println("成功归还车辆：" + vehicle.getLicensePlate());
                    return;
                } else {
                    System.out.println("该车辆未被租用，无法归还。");
                    return;
                }
            }
        }
        System.out.println("未找到指定车辆。");
    }

    private void payRent(List<Vehicle> vehicleList) {
        int totalRent = 0;
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.isRented()) {
                if (vehicle instanceof Car) {
                    totalRent += 50 * vehicle.getRentDays();
                } else if (vehicle instanceof Truck) {
                    if (vehicle.getRentDays() <= 5) {
                        totalRent += 100 * vehicle.getRentDays();
                    } else {
                        totalRent += 100 * 5 + 95 * (vehicle.getRentDays() - 5);
                    }
                } else if (vehicle instanceof Bus) {
                    if (vehicle.getRentDays() <= 5) {
                        totalRent += 80 * vehicle.getRentDays();
                    } else {
                        totalRent += 80 * 5 + (int) (80 * 0.8 * (vehicle.getRentDays() - 5));
                    }
                }
            }
        }
        if (totalRent > 0) {
            System.out.println("您本次需要支付的租金为：" + totalRent + "元，请前往线下收银台付款。");
        } else {
            System.out.println("您没有未支付的租金。");
        }
    }
}

// 管理员类
class Administrator extends User {
    private Scanner scanner;

    public Administrator(String username, String password) {
        super(username, password);
        this.scanner = new Scanner(System.in);
    }

    @Override
    public void operate(List<Vehicle> vehicleList) {
        boolean loop = true;
        while (loop) {
            System.out.println("管理员操作菜单：");
            System.out.println("1. 添加车辆");
            System.out.println("2. 删除车辆");
            System.out.println("3. 修改车辆信息");
            System.out.println("4. 查看营业额");
            System.out.println("5. 退出");
            try {
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice) {
                    case 1:
                        addVehicle(vehicleList);
                        break;
                    case 2:
                        deleteVehicle(vehicleList);
                        break;
                    case 3:
                        modifyVehicle(vehicleList);
                        break;
                    case 4:
                        viewTurnover(vehicleList);
                        break;
                    case 5:
                        loop = false;
                        break;
                    default:
                        System.out.println("无效选择，请重新输入。");
                }
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入。");
                scanner.nextLine();
            }
        }
    }

    private void addVehicle(List<Vehicle> vehicleList) {
        System.out.println("请选择要添加的车辆类型：1. 汽车 2. 卡车 3. 巴士");
        try {
            int vehicleType = scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入车辆车牌：");
            String licensePlate = scanner.nextLine();
            System.out.println("请输入车辆品牌：");
            String brand = scanner.nextLine();
            if (vehicleType == 1) {
                vehicleList.add(new Car(licensePlate, brand));
            } else if (vehicleType == 2) {
                vehicleList.add(new Truck(licensePlate, brand));
            } else if (vehicleType == 3) {
                vehicleList.add(new Bus(licensePlate, brand));
            } else {
                System.out.println("无效的车辆类型选择。");
            }
            System.out.println("成功添加车辆：" + licensePlate);
        } catch (Exception e) {
            System.out.println("输入错误，请重新输入。");
            scanner.nextLine();
        }
    }

    private void deleteVehicle(List<Vehicle> vehicleList) {
        System.out.println("请输入要删除的车辆车牌：");
        String deletePlate = scanner.nextLine();
        boolean found = false;
        for (int i = 0; i < vehicleList.size(); i++) {
            Vehicle vehicle = vehicleList.get(i);
            if (vehicle.getLicensePlate().equals(deletePlate)) {
                vehicleList.remove(i);
                found = true;
                System.out.println("成功删除车辆：" + deletePlate);
                break;
            }
        }
        if (!found) {
            System.out.println("未找到指定车辆。");
        }
    }

    private void modifyVehicle(List<Vehicle> vehicleList) {
        System.out.println("请输入要修改的车辆车牌：");
        String modifyPlate = scanner.nextLine();
        boolean found = false;
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.getLicensePlate().equals(modifyPlate)) {
                found = true;
                System.out.println("车辆类型：" + vehicle.getClass().getSimpleName() + "，车牌：" + vehicle.getLicensePlate() + "，品牌：" + vehicle.getBrand());
                System.out.println("请输入新的车辆品牌：");
                String newBrand = scanner.nextLine();
                vehicle.brand = newBrand;
                System.out.println("成功修改车辆信息。");
                break;
            }
        }
        if (!found) {
            System.out.println("未找到指定车辆。");
        }
    }

    private void viewTurnover(List<Vehicle> vehicleList) {
        System.out.println("正在计算营业额...");
        int turnover = 0;
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.isRented()) {
                if (vehicle instanceof Car) {
                    turnover += 50 * vehicle.getRentDays();
                } else if (vehicle instanceof Truck) {
                    if (vehicle.getRentDays() <= 5) {
                        turnover += 100 * vehicle.getRentDays();
                    } else {
                        turnover += 100 * 5 + 95 * (vehicle.getRentDays() - 5);
                    }
                } else if (vehicle instanceof Bus) {
                    if (vehicle.getRentDays() <= 5) {
                        turnover += 80 * vehicle.getRentDays();
                    } else {
                        turnover += 80 * 5 + (int) (80 * 0.8 * (vehicle.getRentDays() - 5));
                    }
                }
            }
        }
        try {
            Thread.sleep(3000);
            System.out.println("营业额为：" + turnover);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

// 主类
public class VehicleRentalSystem {
    public static void main(String[] args) {
        List<Vehicle> vehicleList = new ArrayList<>();
        // 预先存入车辆信息
        vehicleList.add(new Car("川R123", "华为"));
        vehicleList.add(new Car("川R456", "小米"));
        vehicleList.add(new Truck("川Rabc", "大众"));
        vehicleList.add(new Truck("川Rdef", "广汽"));
        vehicleList.add(new Bus("川R111", "比亚迪"));
        vehicleList.add(new Bus("川R222", "特斯拉"));

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        User user;
        if (username.equals("admin") && password.equals("admin123")) {
            user = new Administrator(username, password);
        } else {
            user = new VIPUser(username, password);
        }

        if (user.login(username, password)) {
            user.operate(vehicleList);
        } else {
            System.out.println("登录失败，请重新输入。");
        }

        scanner.close();
    }
}
